<template>
  <Model
    :title="row.edit ? '编辑' : '添加'"
    :width="800"
    :show="show"
    @confirm="confirm"
    @cancel="cancel"
  >
    <Form slot="form">
      <FormDoubleItem :labels="['标题', '类型']">
        <a-input
          slot="left"
          v-model="row.title"
          placeholder="请输入内容"
        ></a-input>
        <a-select
          slot="right"
          show-search
          :default-value="row.type"
          placeholder="请选择"
          option-filter-prop="children"
          style="width: 100%"
          @change="(value) => changeValue(value, 'type')"
        >
          <a-select-option v-for="item in newsType" :key="item" :value="item">
            {{ item }}
          </a-select-option>
        </a-select>
      </FormDoubleItem>
      <FormDoubleItem :labels="['状态', '发表时间']">
        <a-select
          slot="left"
          show-search
          :default-value="row.status"
          placeholder="请选择"
          option-filter-prop="children"
          style="width: 100%"
          @change="(value) => changeValue(value, 'status')"
        >
          <a-select-option v-for="item in newsStatus" :key="item" :value="item">
            {{ item }}
          </a-select-option>
        </a-select>
        <a-date-picker
          key="1"
          slot="right"
          v-if="!!row.time"
          show-time
          :defaultValue="moment(row.time, 'YYYY-MM-DD HH:mm:ss')"
          format="YYYY-MM-DD HH:mm:ss"
          @change="onChange"
        />
        <a-date-picker
          key="2"
          slot="right"
          v-if="!row.time"
          show-time
          format="YYYY-MM-DD HH:mm:ss"
          @change="onChange"
        />
      </FormDoubleItem>
      <FormLenItem label="内容">
        <a-textarea
          v-model="row.content"
          :auto-size="{ minRows: 3, maxRows: 5 }"
          placeholder="请输入内容"
        ></a-textarea>
      </FormLenItem>
      <FormDoubleItem :labels="['附件', '图片']">
        <a-upload
          slot="left"
          :file-list="file"
          :multiple="false"
          :remove="(file) => handleRemove(file, 'file')"
          :before-upload="(file) => beforeUpload(file, 'file')"
        >
          <a-button> <a-icon type="upload" /> 选择文件 </a-button>
        </a-upload>
        <a-upload
          slot="right"
          :file-list="url"
          :multiple="false"
          :remove="(url) => handleRemove(url, 'url')"
          :before-upload="(url) => beforeUpload(url, 'url')"
        >
          <a-button> <a-icon type="upload" /> 选择文件 </a-button>
        </a-upload>
      </FormDoubleItem>
    </Form>
  </Model>
</template>

<script>
import Model from "components/common/model/Model";
import Form from "components/common/form/Form";
import FormItem from "components/common/form/formItem/FormItem";
import FormLenItem from "components/common/form/formItem/FormLenItem";
import FormDoubleItem from "components/common/form/formDoubleItem/FormDoubleItem";
import { check, newsType, newsStatus } from "assets/data/other";
import { hasPropertyEmpty } from "common/index";
import moment from "moment";
export default {
  components: {
    Model,
    Form,
    FormDoubleItem,
    FormLenItem,
  },
  props: {
    row: {
      type: Object,
      default() {
        return {};
      },
    },
    show: {
      type: Boolean,
      defalut: false,
    },
  },
  data() {
    return {
      check,
      newsType,
      newsStatus,
      moment,
      file: [],
      url: [],
    };
  },
  methods: {
    onChange(value, dateString) {
      this.row.time = dateString;
    },
    beforeUpload(file, key) {
      this[key] = [file];
      this.row[key] = file;
      return false;
    },
    handleRemove(file, key) {
      const index = this[key].indexOf(file);
      const newFileList = this[key].slice();
      newFileList.splice(index, 1);
      this[key] = newFileList[0];
      this.row[key] = "";
    },
    changeValue(value, key) {
      this.row[key] = value;
    },
    confirm() {
      const row = this.row;
      this.$emit("confirm", {
        ...row,
        type: newsType.indexOf(row.type),
        status: newsStatus.indexOf(row.status),
      });
    },
    cancel() {
      this.$emit("cancel");
    },
  },
};
</script>

<style lang="less" scoped>
.t-form {
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-items: center;
  padding-right: 30px;
  .ant-btn {
    width: 200px !important;
  }

  textarea[class="ant-input"] {
    width: 570px !important;
  }
}
</style>